package com.whiteblue.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.whiteblue.Interceptor.LoginInterceptor;
import com.whiteblue.model.Book;
import com.whiteblue.model.Category;
import com.whiteblue.model.User;

import java.util.Enumeration;
import java.util.Random;

/**
 * Created by WhiteBlue on 15/4/4.
 */
public class IndexController extends Controller {
    private static final String client_id = "";
    private static final String client_secret = "";
    private static final String redirect_uri = "";

    public void index() {
        this.setAttr("books", Book.dao.getForIndex());
        this.render("/index.html");
    }

    public void login() {
        this.render("/login.html");
    }

    public void register() {
        this.render("/register.html");
    }

    public void conformQQ() {
        Random random = new Random();
        int state = random.nextInt(10);
        this.redirect("https://graph.qq.com/oauth2.0/authorize?response_type=code&state=" + state + "&client_id=" + client_id + "&redirect_uri=" + redirect_uri);
    }

    public void loginQQ() {
        if (this.getPara("code") != null) {
            String code = this.getPara("code");
            String redirect_uri = "";
            this.redirect("https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + client_id + "&client_secret=" + client_secret + "&code=" + code + "&redirect_uri=" + redirect_uri);
        } else if (this.getPara("access_token") != null) {
            String access_token = this.getPara("access_token");
            this.redirect("https://graph.qq.com/oauth2.0/me?access_token＝" + access_token);
        }
    }

    @Before(LoginInterceptor.class)
    public void addBook(){
        this.setAttr("category", Category.dao.listAll());
        this.render("/book_add.html");
    }

    @Before(LoginInterceptor.class)
    public void editInfo(){
        this.render("/userInfoEdit.html");
    }

}
